$(function(){
	var addressJson=allarea()
	$('.back').on('click', function() {
		window.history.back();
	});
	$('.keep').on('click', function() {
		{if $item.name}
		sendForm($(this), 1,window.location.href);
		{else/}
		sendForm($(this), 1,'{:url("index")}');
		{/if}
	});
	$('.company_name').on('click', function() {
		choosecar("{:url('company/index',['op'=>'search'])}")
	});
	var provinceid = $('.province').data('index');
	var cityid = $('.city').data('index');
	var countyid = $('.county').data('index');

	//省市县
	var province = "";
	
	var city0;
	var county0;
	for(var i = 0; i < addressJson.length; i++) {
		province += `<option value="${i}" >${addressJson[i].name}</option>`;
	}
	
	$(".province").html(province);
	$(".province").on('change', function() {
		var city;
		for(var j = 0; j < addressJson[this.value].city.length; j++) {
			city += `<option value="${j}">${addressJson[this.value].city[j].name}</option>`;
		}
		$(".city").html(city);
	});
	$('.birthday').on('change',function(){
			var newdata=new Date()
			var buydata=new Date($(this).val())
			var year=parseInt((newdata-buydata)/1000/3600/24/365)
			$('.age').val(year)
	})
	$('.user_name').on('click', function() {
		choosecar("{:url('user/index',['op'=>'search'])}")
	});
	$(".city").on('change', function() {
		var county;
		for(var z = 0; z < addressJson[$(".province")[0].value].city[this.value].district.length; z++) {
			county += `<option value="${z}">${addressJson[$(".province")[0].value].city[this.value].district[z].name}</option>`;
		};
		$(".county").html(county);
	});
	if(provinceid == 0) {
		for(var i1 = 0; i1 < addressJson[0].city.length; i1++) {
			city0 += `<option value="${i1}">${addressJson[0].city[i1].name}</option>`;
		}
		$(".city").html(city0);
		for(var i2 = 0; i2 < addressJson[0].city[0].district.length; i2++) {
			county0 += `<option value="${i2}">${addressJson[0].city[0].district[i2].name}</option>`;
		}
		$(".county").html(county0);
	} else {
		$('.province').val(provinceid);
		var city1;
		for(var j1 = 0; j1 < addressJson[provinceid].city.length; j1++) {
			city1 += `<option value="${j1}">${addressJson[provinceid].city[j1].name}</option>`;
		}
		$(".city").html(city1);
		$('.city').val(cityid);
		var county1;
		for(var z1 = 0; z1 < addressJson[provinceid].city[cityid].district.length; z1++) {
			county1 += `<option value="${z1}">${addressJson[provinceid].city[cityid].district[z1].name}</option>`;
		}
		$(".county").html(county1);
		$('.county').val(countyid); 
	}
	
	
	if($('.company_id').val()){
		$.ajax({
			method:'POST',
			url:"{:url('admin/used/department')}",
			data:{id:$('.company_id').val()},
			success:function(res){
				console.log(res)
				var department;
				for (var d in res){
					
					department+=`<option value="${res[d].department_id}">${res[d].name}</option>`
				}
				$('.department').html(department)
				$('.department').val($('.department').data('id'))
			}
			
		})
	}else{
	}
    $('.is_check').click(function(){
        var a = $(this).val();
        if(a==0){
            $("input:radio[name='is_instead'][value='0']").attr("checked",true);
        }
        if(a==1){
            $("input:radio[name='is_instead'][value='1']").attr("checked",true);
        }
    });
	$('.sex').val($('.sex').data('idx'))
	$('.source').val($('.source').data('idx'))
})
